package com.odm.communitymanage.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.odm.communitymanage.R;

/**
 * @description: 普通ios风对话框
 * @author: ODM
 * @date: 2020/4/25
 */
public class CommonDialog extends Dialog {

    private Context mContext;

    private String title;
    private String content;
    private String cancel;
    private String ensure;

    private TextView tvTitle;
    private TextView tvContent;
    private Button btnEnsure;
    private Button btnCancel;

    private View.OnClickListener onEnsureClickListener;
    private View.OnClickListener onCancelClickListener;
    
    
    public CommonDialog(@NonNull Context context) {
        super(context, R.style.dialog_Common);
        mContext = context;
        setContentView(R.layout.dialog_layout_common);
        init();
    }

    private void init(){
        tvTitle = findViewById(R.id.dialog_title);
        tvContent = findViewById(R.id.dialog_content);
        btnCancel = findViewById(R.id.dialog_cancel);
        btnEnsure = findViewById(R.id.dialog_ensure);
    }



    /**
     * 设置标题 默认没有标题
     * @param title
     */
    public void setTitleText(String title) {
        this.title = title;
    }
    /**
     * 设置内容 默认为空
     *
     * @param content
     */
    public void setContentText(String content) {
        this.content = content;
    }
    /**
     * 设置确定按钮内容 默认为确定
     *
     * @param ensure
     */
    public void setEnsureText(String ensure) {
        this.ensure = ensure;
    }
    /**
     * 设置取消按钮内容 默认为取消
     *
     * @param cancel
     */
    public void setCancelText(String cancel) {
        this.cancel = cancel;
    }

    /**
     * 确定按钮事件监听 默认是dismiss对话框
     *
     * @param onEnsureClickListener
     */
    public void setOnEnsureClickListener(View.OnClickListener onEnsureClickListener) {
        this.onEnsureClickListener = onEnsureClickListener;
    }
    /**
     * 取消按钮事件监听 默认是dismiss对话框
     *
     * @param onCabcelClickListener
     */
    public void setOnCancelClickListener(View.OnClickListener onCabcelClickListener) {
        this.onCancelClickListener = onCabcelClickListener;
    }
    


    /**
     * 默认点击事件，点击弹框消失
     */
    private View.OnClickListener onClickListenerDismiss = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };
    
    
    private void setButtonOnClickListener(Button mButton , View.OnClickListener listener){
        mButton.setOnClickListener(listener);
    }
    
    private void setTextViewText(TextView tv , String text){
        if(tv!= null && text != null){
            tv.setText(text);
        }
    }
    


    /**
     * 重写show方法
     */
    @Override
    public void show() {
        if (TextUtils.isEmpty(title)) {
            //默认没有标题
            tvTitle.setVisibility(View.GONE);
        } else {
            //默认不设置内容，则内容太为空
            tvTitle.setVisibility(View.VISIBLE);
            setTextViewText(tvTitle, title);
        }
        if (TextUtils.isEmpty(cancel)) {
            //默认取消按钮文字为"取消"
            cancel = mContext.getString(R.string.cancel);
        }
        if (TextUtils.isEmpty(ensure)) {
            //默认确认按钮文字为"确认"
            ensure = mContext.getString(R.string.ensure);
        }
        //设置文字信息
        setTextViewText(tvContent, content);
        setTextViewText(btnCancel, cancel);
        setTextViewText(btnEnsure, ensure);
        //设置点击事件
        setButtonOnClickListener(btnCancel, onCancelClickListener);
        setButtonOnClickListener(btnEnsure, onEnsureClickListener);
        super.show();
    }




}
